package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.model.settlement.SettlementRule;
import com.tiancheng.trade.settlement.service.settlement.ISettlementRuleService;
import com.tiancheng.trade.settlement.util.Res;
import com.tiancheng.trade.settlement.vo.settlement.api.GetSettlementRuleVO;
import com.tiancheng.trade.settlement.vo.settlement.api.SettlementRuleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 清分规则
 *
 * @author lzh
 */
@RestController
@RequestMapping("/admin/settlementrule")
@Validated
@Slf4j
public class SettlementRuleAdminController {

    @Autowired
    ISettlementRuleService settlementService;

    /**
     * 按id查询清分规则信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(settlementService.findById(id));
    }

    /**
     * 查询清分规则列表信息
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/findPageList")
    public Result findPageList(GetSettlementRuleVO settlementRuleVO) throws BusinessException {
        PageInfo<SettlementRule> result = settlementService.getSettlementRule(settlementRuleVO);
        return Result.success(result);
    }

    /**
     * 查询清分规则信息
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/findSettlementRuleList")
    public Result findSettlementRuleList(GetSettlementRuleVO settlementRuleVO) throws BusinessException {
        List<SettlementRule> result = settlementService.getSettlementRuleList(settlementRuleVO);
        return Result.success(result);
    }

    /**
     * 按照生效时间和优先级查询是否存在优先级相同的有效规则
     * 前端需要有值返回1 无值返回0
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/findSettlementRuleByLevel")
    public Result findSettlementRuleByLevel(SettlementRule settlementRuleVO) throws BusinessException {
        List<SettlementRule> result = settlementService.findSettlementRuleByLevel(settlementRuleVO);
        if (result.size() > 0) {
            return Result.success(1);
        }
        return Result.success(0);
    }

    /**
     * 查询当前日期未生效的清分规则信息
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/getIneffectiveRuleList")
    public Result getIneffectiveRuleList(GetSettlementRuleVO settlementRuleVO) throws BusinessException {
        List<SettlementRule> result = settlementService.getIneffectiveRuleList(settlementRuleVO);
        return Result.success(result);
    }

    /**
     * 商品编码，优先级，生效时间大于当前时间并且规则失效时间小于当前时间
     * 按优先级排序
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/getSettlementByCodeLevel")
    public Result getSettlementByCodeLevel(SettlementRuleVO settlementRuleVO) throws BusinessException {
        SettlementRule result = settlementService.getSettlementByCodeLevel(settlementRuleVO);
        return Result.success(result);
    }

    /**
     * 新增清分规则信息
     *
     * @param settlementRule
     * @return
     */
    @PostMapping("/")
    public Result add(@RequestBody SettlementRule settlementRule) throws BusinessException {
        return Res.judge(settlementService.add(settlementRule), "新增");
    }

    /**
     * 修改清分规则信息
     *
     * @param settlementRule
     * @return
     */
    @PutMapping("/")
    public Result update(@RequestBody SettlementRule settlementRule) throws BusinessException {
        Assert.isTrue(settlementRule.getId() != null, "参数错误~");
        return Res.judge(settlementService.update(settlementRule), "修改");
    }

    /**
     * 作废清分规则信息
     *
     * @param ruleId
     * @return
     */
    @DeleteMapping("/{ruleId}")
    public Result updateCancel(@PathVariable Long ruleId) throws BusinessException {
        Assert.isTrue(ruleId != null, "参数错误~");
        return Res.judge(settlementService.updateCancel(ruleId), "作废");
    }

    /**
     * 定时生效
     *
     * @return
     */
    @PostMapping("/autoEffective")
    public Result autoEffective() throws BusinessException {
        try {
            int cnt = settlementService.autoEffective();
            if (cnt == 1) {
                return Result.success("定时生效成功");
            } else if (cnt == 0) {
                return Result.success("无定时生效数据");
            } else {
                throw new Exception("定时生效失败");
            }
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 定时失效
     *
     * @return
     */
    @PostMapping("/autoExpire")
    public Result autoExpire() throws BusinessException {
        try {
            int cnt = settlementService.autoExpired();
            if (cnt == 1) {
                return Result.success("定时失效成功");
            } else if (cnt == 0) {
                return Result.success("无定时失效数据");
            } else {
                throw new Exception("定时失效失败");
            }
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询清分规则列表信息
     *
     * @param settlementRuleVO
     * @return
     */
    @PostMapping("/export")
    public Result settlementRuleExport(@RequestBody GetSettlementRuleVO settlementRuleVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        settlementService.export(settlementRuleVO, request, response);
        return Result.success("导出成功");
    }

}
